package org.example.menu;

import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static java.util.stream.Collectors.*;

public class DishTest {

    //27.先按菜单中的类型分组，再按热量（低、中、高）分组
    static enum Calorie {
        D, Z, G
    }

    public static void main(String[] args) {


        List<Dish> menu = Arrays.asList(
                new Dish("牛肉", 100, Dish.Type.MEAN),
                new Dish("猪肉", 200, Dish.Type.MEAN),
                new Dish("羊肉", 300, Dish.Type.MEAN),
                new Dish("香蕉", 400, Dish.Type.OTHER),
                new Dish("香蕉", 500, Dish.Type.OTHER)
        );

        Map<Dish.Type, Map<Calorie, List<Dish>>> menus = menu.stream().collect(groupingBy(Dish::getType, groupingBy(dish -> {
            int calorie = dish.getCalorie();
            if (calorie < 200) {
                return Calorie.D;
            } else if (calorie > 400) {
                return Calorie.G;
            } else {
                return Calorie.Z;
            }
        })));

        menus.keySet().forEach(
                Type -> System.out.println(
                        Type + "\n" + menus.get(Type)));
        //28.计算菜单中每种类型的菜的数量
        Map<Dish.Type, Long> typeNum = menu.stream().
                collect(groupingBy(Dish::getType, counting()));
        System.out.println(typeNum);
//        29.查找每个类型中热量最高的菜

        Map<Dish.Type, Optional<Dish>> fatestDishes = menu.stream().collect(groupingBy(Dish::getType, maxBy(Comparator.comparingInt(Dish::getCalorie))));
        System.out.println(fatestDishes);
        //30.求出每种类型的菜的热量和
        Map<Dish.Type, IntSummaryStatistics> map = menu.stream()
                .collect(groupingBy(Dish::getType, summarizingInt(Dish::getCalorie)));
        for (Map.Entry<Dish.Type, IntSummaryStatistics> entry :
                map.entrySet()) {
            System.out.println("type=" + entry.getKey() + "  " + "sum=" + entry.getValue().getSum());

        }
    }

}
